/*
 * @Author: your name
 * @Date: 2021-06-07 13:45:38
 * @LastEditTime: 2021-06-16 17:50:45
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \UDP\CLIENT_\manager.cpp
 */
#include "manager.h"

Manager::Manager()
{
    this->m_user = NULL;
	this->isLogin = false;
}

Manager::~Manager()
{
    //释放内存
    if(this->m_user)
    {
        free(this->m_user);
        this->m_user = NULL;
    }
}

int Manager::input_Choice()
{
	string num_str;
	int num;
	bool isNum = false;
	while(!isNum)
	{
		num = 0;
		cin >> num_str;
		isNum = true;
		//判断是否是合法字符（数字）
		for(int i=0; i<num_str.length(); i++)
		{
			num = (num + num_str.at(i)-'0')*10;
			//发现非法字符，请求用户重新输入
			if(num_str.at(i) < '0' || num_str.at(i) > '9')
			{
				isNum = false;
				break;
			}
		}
		// cout << "num=" << num << endl;
		if(!isNum)
		{
			cout << "输入有误，请重新输入" << endl;
		}
		else
		{
			return num/10;
		}
	}
}

void Manager::regist(ClientSocket& c_socket)
{
	while(true)
	{
		cout << "请输入注册账户名：" << endl;
		string name = this->get_Name();
		//吃掉回车
		cin.get();
		cout << "请输入您的密码" << endl;
		string passwd = this->get_Passwd();
		//验证账户名是否已经存在
		info nameInfo;
		//将名字写入info
		strcpy(&nameInfo.name[0],name.data());
		//写入密码
		strcpy(&nameInfo.passwd[0],passwd.data());
		//设置标识码
		nameInfo.com_src = CLINET_SEND;
		nameInfo.opcode = _REGISTER;
		//清理缓存
		c_socket.clear_Cache();
		//获取长度
		c_socket.send_length = sizeof(nameInfo);
		//将待发送信息写入缓存
		memcpy(c_socket.send_buf,&nameInfo,c_socket.send_length);
		//发送消息
		info* test_info = (info*)c_socket.send_buf;
		cout << "opcode:" << test_info->opcode << endl;
		bool getRecv = c_socket.send_To_Server();
		//收到消息
		if(getRecv)
		{
			info* getInfo = (info*)c_socket.recv_buf;
			int recv_statu = getInfo->status_code;
			if(recv_statu == _REGISTER_ERROR)
			{
				cout << "该用户名已经存在，请更换一个用户名再次尝试" << endl;
			}
			else
			{
				cout << "注册成功" << endl;
				break;
			}
		}
		//超时
		else
		{
			cout << "服务器繁忙，注册失败" << endl;
			break;
		}
	}
	
}

bool Manager::login(ClientSocket& c_socket)
{
    //获取用户名
	cout << "请输入你的账户名" << endl;
    string u_name = this->get_Name();
    cin.get();//吃掉回车
    //获取用户密码
	cout << "请输入你的密码" << endl;
    string u_passwd = this->get_Passwd();

    //cout << "name:" << u_name << " passwd:" << u_passwd << endl;
    //验证用户名用户密码

	//构造通信子串
    info login_info;
	//通信方及通信操作
	login_info.com_src = CLINET_SEND;
	login_info.opcode = _LOGIN;
	//登录的账户名和密码
	strcpy(&login_info.name[0],(char*)u_name.data());
	strcpy(&login_info.passwd[0],(char*)u_passwd.data());

	//this->show_struct_info(login_info);
	c_socket.send_length = sizeof(login_info);

	// cout << "长度：" << c_socket.send_length << endl;
	/*准备通信*/
	
	//清理缓存
	c_socket.clear_Cache();
	//将待发送信息写入缓存
	memcpy(c_socket.send_buf,&login_info,c_socket.send_length);
	// cout << "准备发送长度：" << c_socket.send_length << endl;
	//发送并等待接收服务器的反馈
	bool getRecv = c_socket.send_To_Server();
	//cin.get();
	//收到反馈
	if(getRecv)
	{
		//判断type类型
		info* tinfo = (info*)c_socket.recv_buf;
		switch (tinfo->type)
		{
			//登录错误码
		case _LOGIN_ERROR:
			cout << "账号或密码错误，登录失败" << endl;
			break;
			//商家登录
		case _LOGIN_MERCHANT:
		cout << "欢迎登录（商家）：" << u_name << endl;
		this->m_user = new Merchant(u_name,u_passwd,tinfo->balance);
		this->isLogin = true; //登录状态更新
		break;
			//消费者登录
		case _LOGIN_CONSUMER:
		cout << "欢迎登录（客户）：" << u_name << endl;
		this->m_user = new Consumer(u_name,u_passwd,tinfo->balance,tinfo->cartnum,tinfo->cartIndexArray,tinfo->indexNumArray);
		this->isLogin = true; //登录状态更新
		break;
			//default
		default:
			break;
		}
	}
	//服务器繁忙没有收到反馈
	else
	{
		cout << "服务器繁忙，登录失败" << endl;
	}
	
}

void Manager::show_Commodity(ClientSocket& c_socket)
{
	cout << "show" << endl;
	//构造通信子串
	info tinfo;
	
	//查询信息
	tinfo.opcode = _SHOW_ALL_COMMODITY;


	//获取长度
	c_socket.send_length = sizeof(tinfo);
	//清理缓存
	c_socket.clear_Cache();
	//将待发送信息写入缓存
	memcpy(c_socket.send_buf,&tinfo,c_socket.send_length);
	//发送并等待接收服务器的反馈
	bool getRecv = c_socket.send_To_Server();
	//反馈
	if(getRecv)
	{
		//吃回车
		cin.get();
		info* r_info = (info*)c_socket.recv_buf;
		int num = r_info->com_num;
		cout << "商品信息如下" << endl;
		Guide().show_Com_Header();
		for(int i=0; i<num; i++)
		{
			cout <<r_info->com_array[i].identifier << "\t"
				<< r_info->com_array[i].cname << "\t"
				<< r_info->com_array[i].brand << "\t"
				<< r_info->com_array[i].classify << "\t"
				<< r_info->com_array[i].price << "\t"
				<< r_info->com_array[i].remain << "\t"
				<< r_info->com_array[i].frozen << "\t"
				<< r_info->com_array[i].others << endl;
		}
	}
	//繁忙
	else
	{

	}
}

void Manager::exit_exe()
{
	cout << "再见，欢迎再次登录" << endl;
	cin.get();
	cin.get();
	system("clear");
	exit(-1);
}

void Manager::search_Commodity(ClientSocket& c_socket)
{
	//三种搜索方式
	cout << "检索方式如下" << endl;
	cout << SEARCH_PRICE << ".按价格区间检索" << endl;
	cout << SEARCH_CLASS << ".按种类名称检索" << endl;
	cout << SEARCH_NAME << ".按商品名称检索" << endl;
	cout << "请输入您的检索方式" << endl;
	int choice;
	choice = this->input_Choice();
	//传输串
	info s_info;
	s_info.opcode = _SEARCH_COMMODITY;
	s_info.searchcode = choice;
	switch (choice)
	{
	case SEARCH_PRICE:
		{
			cout << "请输入最低价格" << endl;
			s_info.min_price = this->get_Mid_Num(0.0,99999.0);
			cout << "请输入最高价格" << endl;
			s_info.max_price = this->get_Mid_Num(s_info.min_price,99999.0);
			break;
		}
	case SEARCH_CLASS:
		{
			cout << "请输入种类名称" << endl;
			cin >> s_info.classify;
			break;
		}
	case SEARCH_NAME:
		{
			cout << "请输入商品名称" << endl;
			cin >> s_info.sc_name;
			break;	
		}
	default:
		choice = -1; //标志无法查询
		cout << "非法输入" << endl;
		break;
	}

	if(choice != -1)
	{
		//准备发送消息
		
		//这里将用户名添加
		memcpy(s_info.name,this->m_user->get_Name().data(),strlen(this->m_user->get_Name().data()));
		//获取长度
		c_socket.send_length = sizeof(s_info);
		//清理缓存
		c_socket.clear_Cache();
		//将待发送信息写入缓存
		memcpy(c_socket.send_buf,&s_info,c_socket.send_length);

		cout << "searchcode:" << s_info.searchcode << endl; 

		//发送并等待接收服务器的反馈
		bool getRecv = c_socket.send_To_Server();
		//处理回复
		if(getRecv)
		{
			info* r_info = (info*)c_socket.recv_buf;
			int num = r_info->com_num;
			cout << "num:" << num << endl;
			cout << "检索信息如下" << endl;
			Guide().show_Com_Header();
			for(int i=0; i<num; i++)
			{
				cout <<r_info->com_array[i].identifier << "\t"
					<< r_info->com_array[i].cname << "\t"
					<< r_info->com_array[i].brand << "\t"
					<< r_info->com_array[i].classify << "\t"
					<< r_info->com_array[i].price << "\t"
					<< r_info->com_array[i].remain << "\t"
					<< r_info->com_array[i].frozen << "\t"
					<< r_info->com_array[i].others << endl;
			}
		}
		else
		{
			cout << "服务器繁忙，检索失败" << endl;
		}
	}
	else
	{
		//处理结束
	}
	cin.get();
}

void Manager::show_Cart(ClientSocket& c_socket)
{
	int cart_num = this->m_user->get_Commodity_Num(); //购物车商品数目
	int* cart_index_array = this->m_user->get_Commodity_Index_Array(); //购物车商品唯一标识数组
	int* cart_num_array = this->m_user->get_Commodity_Index_Num_Array(); //购物车对应商品数目
	if(cart_num == 0)
	{
		cout << "你的购物车空空如也" << endl;
	}
	else
	{
		cout << "购物车商品下标及对应数目如下" << endl;
		for(int i=0; i<cart_num; i++)
		{
			cout << "identifier:" << cart_index_array[i]
				<< " num:" << cart_num_array[i] << endl;
			//请求当前商品信息
			commodity com;
			this->get_Com_By_Id(cart_index_array[i],c_socket,com);
			cout << "名称：" << com.cname << "\t"
				<< "品牌：" << com.brand << "\t"
				<< "分类：" << com.classify << "\t"
				<< "价格：" << com.price << "\t"
				<< "剩余数目：" << com.remain << "\t"
				<< "冻结数目：" << com.frozen << "\t"
				<< "其他信息：" << com.others << endl;
		}
	}	
	cin.get();
}

bool Manager::add_Cart(ClientSocket& c_socket)
{
	//输入要添加的商品标识
	cout << "请输入你要添加商品的标识：" << endl;
	int iden;
	cin >> iden;
	//搜索该商品
	commodity com;
	int fnum = this->get_Com_By_Id(iden,c_socket,com);
	if(fnum == 0)
	{
		cout << "商品标识错误或服务器繁忙，添加购物车失败" << endl;
		return false;
	}
	else
	{
		//获取添加商品数目
		cout << "请输入你要添加进购物车是商品数目：0-" << com.remain << endl;
		int cnum = this->get_Mid_Num(0,com.remain);

		//准备通知服务器修改该用户购物车和部分商品信息
		//通信串
		info* s_info = (info*)c_socket.send_buf;
		s_info->opcode = _ADD_CART;
		// 购物车信息更改
		int cart_num = this->m_user->get_Commodity_Num();
		int* cart_array = this->m_user->get_Commodity_Index_Array();
		int* cart_index_array = this->m_user->get_Commodity_Index_Num_Array();
		//购物车数目+1
		this->m_user->set_Commodity_Num(cart_num+1);
		//添加商品信息
		cart_array[cart_num] = iden;
		cart_index_array[cart_num] = cnum;
		//将新购物车信息写入到s_info
		s_info->cartnum = cart_num+1;
		//这里将用户名添加
		strcpy(&s_info->name[0],(char*)this->m_user->get_Name().data());
		for(int i=0; i<s_info->cartnum; i++)
		{
			s_info->cartIndexArray[i] = cart_array[i];
			s_info->indexNumArray[i] = cart_index_array[i];
		}
		//更新商品信息
		s_info->identifier = iden;
		s_info->remain_num = com.remain - cnum;
		s_info->frozen_num = com.frozen + cnum;
		//发送
		bool getRecv = c_socket.send_To_Server();
		if(getRecv)
		{
			info* r_info = (info*)c_socket.recv_buf;
			if(r_info->status_code == _ADD_CART_OK)
			{
				cout << "添加购物车成功" << endl;
			}
			else
			{
				cout << "添加失败" << endl;
				this->m_user->set_Commodity_Num(cart_num);
			}
			//this->show_Cart(c_socket);
		}
		else
		{
			cout << "添加购物车失败" << endl;
			//还原购物车数目
			this->m_user->set_Commodity_Num(cart_num);
		}
	}
	cin.get();
}

bool Manager::modify_Cart(ClientSocket& c_socket)
{
	//购物车商品数目
	int cart_num = this->m_user->get_Commodity_Num();
	//购物车为空
	if(cart_num == 0)
	{
		cout << "购物车空空如也" << endl;
		return false;
	}
	//输入修改编号
	cout << "请输入你要修改的商品购物车编号" << endl;
	int m_index;
	m_index = this->get_Mid_Num(0,cart_num-1);
	int* cart_array = this->m_user->get_Commodity_Index_Array();
	int* cart_index_num = this->m_user->get_Commodity_Index_Num_Array();

	//被修改商品的id
	int iden = cart_array[m_index];

	//被修改商品目前购物车的数目
	int iden_num = cart_index_num[m_index];
	
	//获取该商品目前总存货
	commodity com;
	this->get_Com_By_Id(iden,c_socket,com);
	int com_remain = com.remain;
	int com_frozen = com.frozen;

	cout << "该商品目前信息如下" << endl;
	cout << "商品id:" << iden 
		<< " 购物车该商品数目:" << iden_num
		<< " 名称：" << com.cname
		<< " 品牌：" << com.brand
		<< " 种类：" << com.classify
		<< " 价格：" << com.price
		<< " 其他信息:" << com.others << endl;
	//限定修改范围
	int max_num = com_remain + iden_num;
	cout << "请输入你要修改的数目：" << endl;
	int new_num = this->get_Mid_Num(0,max_num);

	//通信
	info* s_info = (info*)c_socket.send_buf;
	//可以和添加购物车使用一个接收函数,不过还是分开吧，日志不好处理，减少耦合
	s_info->opcode = _MODIFY_CART;
	//添加用户名
	strcpy(&s_info->name[0],(char*)this->m_user->get_Name().data());
	s_info->cartnum = cart_num;
	//将原购物车复制
	for(int i=0; i<s_info->cartnum; i++)
	{
		s_info->cartIndexArray[i] = cart_array[i];
		s_info->indexNumArray[i] = cart_index_num[i];
	}
	//更改的地方单独赋值
	s_info->indexNumArray[m_index] = new_num;
	//更新商品信息
	s_info->identifier = iden;
	s_info->remain_num = com_remain + iden_num - new_num;
	s_info->frozen_num = com_frozen + new_num - iden_num;
	//发送
	bool getRecv = c_socket.send_To_Server();
	//接收到信息
	if(getRecv)
	{
		info* r_info = (info*)c_socket.recv_buf;
		if(r_info->status_code == _ADD_CART_OK)
		{
			cout << "修改购物车成功" << endl;
			cart_index_num[m_index] = new_num;
		}
		else
		{
			cout << "修改购物车失败" << endl;
		}
		//this->show_Cart(c_socket);
	}
	else
	{
		cout << "修改购物车失败" << endl;
		//还原购物车数目
		this->m_user->set_Commodity_Num(cart_num);
	}
	cin.get();
}

bool Manager::create_Order(ClientSocket& c_socket)
{
	//购物车信息
	int cart_num = this->m_user->get_Commodity_Num();
	int* cart_index = this->m_user->get_Commodity_Index_Array();
	int* cart_index_num = this->m_user->get_Commodity_Index_Num_Array();
	if(cart_num == 0)
	{
		cout << "您的购物车为空" << endl;
		cin.get();
		return false;
	}
	//拷贝一份购物车信息
	int cur_cart_num = cart_num;
	int cur_cart_index[40];
	int cur_cart_index_num[40];
	for(int i=0; i<cur_cart_num; i++)
	{
		cur_cart_index[i] = cart_index[i];
		cur_cart_index_num[i] = cart_index_num[i];
	}
	//记录选中的商品信息，修改库存
	int com_num = 0;
	int id[40];
	int frozen_sub[40];
	//总价
	double sum_money = 0;
	//选择要添加进订单的购物车商品
	while(true)
	{
		//购物车为空，强制退出
		if(cur_cart_num == 0)
		{
			cout << "购物车已经为空" << endl;
			break;
		}
		//选取购物车编号
		cout << "目前购物车信息如下" << endl;
		for(int i=0; i<cur_cart_num; i++)
		{

			cout << "购物车编号：" << i << " identifier:" << cur_cart_index[i]
			<< " num:" << cur_cart_index_num[i] << endl;
			//请求当前商品信息
			commodity com;
			this->get_Com_By_Id(cur_cart_index[i],c_socket,com);
			cout << "名称：" << com.cname << "\t"
				<< "品牌：" << com.brand << "\t"
				<< "分类：" << com.classify << "\t"
				<< "价格：" << com.price << "\t"
				<< "剩余数目：" << com.remain << "\t"
				<< "冻结数目：" << com.frozen << "\t"
				<< "其他信息：" << com.others << endl;
		}
		cout << "请输入您要加入订单的购物车编号：" << endl;
		int order_num = this->get_Mid_Num(0,cur_cart_num-1);
		//获取选取的商品信息
		commodity com;
		this->get_Com_By_Id(cur_cart_index[order_num],c_socket,com);
		//记录商品信息
		id[com_num] = com.identifier;
		frozen_sub[com_num] = cur_cart_index_num[order_num];
		com_num++;
		//计算总价
		sum_money += com.price*cur_cart_index_num[order_num];
		//更新购物车信息，删去刚选中的商品
		for(int i=order_num; i<cur_cart_num-1; i++)
		{
			cur_cart_index[i] = cur_cart_index[i+1];
			cur_cart_index_num[i] = cur_cart_index_num[i+1];
		}
		cur_cart_num--;
		//后续选项
		cout << "输入1继续添加商品进订单，输入0添加完毕" << endl;
		cout << "请输入你的选择:" << endl;
		int c_select;
		cin >> c_select;
		if(c_select == 0)
		{
			cout << "订单选择完毕" << endl;
			break;
		}
		else
		{
			cout << "继续添加订单" << endl;
		}
	}
	//信息打印模块
	{
		//现在选择完订单后所有状况
		cout << "订单信息：" << endl;
		cout << "总金额:" << sum_money << endl;
		// 订单商品信息
		cout << "订单商品如下：" << endl;
		for(int i=0; i<com_num; i++)
		{
			cout << "订单编号：" << i << " 数目：" << frozen_sub[i];
			commodity com;
			this->get_Com_By_Id(id[i],c_socket,com);
			cout << "名称：" << com.cname << "\t"
					<< "品牌：" << com.brand << "\t"
					<< "分类：" << com.classify << "\t"
					<< "价格：" << com.price << "\t"
					<< "剩余数目：" << com.remain << "\t"
					<< "冻结数目：" << com.frozen << "\t"
					<< "其他信息：" << com.others << endl;
		}
		//购物车情况
		cout << "目前购物车信息如下" << endl;
		for(int i=0; i<cur_cart_num; i++)
		{

			cout << "购物车编号：" << i << " identifier:" << cur_cart_index[i]
			<< " num:" << cur_cart_index_num[i] << endl;
			//请求当前商品信息
			commodity com;
			this->get_Com_By_Id(cur_cart_index[i],c_socket,com);
			cout << "名称：" << com.cname << "\t"
				<< "品牌：" << com.brand << "\t"
				<< "分类：" << com.classify << "\t"
				<< "价格：" << com.price << "\t"
				<< "剩余数目：" << com.remain << "\t"
				<< "冻结数目：" << com.frozen << "\t"
				<< "其他信息：" << com.others << endl;
		}
	}

	//准备构造通信包
	info* s_info = (info*)c_socket.send_buf;
	//填充基本信息
	s_info->opcode = _CREATE_ORDER; //操作码
	strcpy(&s_info->name[0],(char*)this->m_user->get_Name().data()); //用户名
	s_info->newbalance = this->m_user->get_Balance() - sum_money;
	//支付确认
	cout << "是够确定支付：" << endl
		<< "输入0:取消支付" << endl
		<< "输入1：确定支付" << endl;
	int isPay;
	cin >> isPay;
	if(isPay == 0)
	{
		cout << "订单支付已取消" << endl;
		cin.get();
		return false;
	}
	else
	{
		if(s_info->newbalance < 0)
		{
			cout << "余额不足，请充值后再次尝试购买" << endl;
			cin.get();
			return false;
		}
	}

	s_info->order_money = sum_money; //购买金额
	//填充用户当前购物车情况
	for(int i=0; i<cur_cart_num; i++)
	{
		s_info->cartIndexArray[i] = cur_cart_index[i];
		s_info->indexNumArray[i] = cur_cart_index_num[i];
	}
	s_info->cartnum = cur_cart_num;
	//填充商品更换信息，主要是冻结数目
	/*
		这里不能直接修改冻结数目，而是应该独立修改减少的数目
		因为我的购物车允许添加同样的商品多次
	*/
	s_info->com_num = com_num;
	for(int i=0; i<com_num; i++)
	{
		s_info->com_array[i].identifier = id[i];
		s_info->com_array[i].frozen = frozen_sub[i];
	}
	//填充完毕，发送
	bool getRecv = c_socket.send_To_Server();
	//接收
	if(getRecv)
	{
		info* r_info = (info*)c_socket.recv_buf;
		if(r_info->status_code == _CREATE_ORDER_PAY_OK)
		{
			cout << "订单支付成功" << endl;
			//将用户的购物车商品数目，购物车商品信息更改到支付订单后
			this->m_user->set_Commodity_Num(cur_cart_num);
			this->m_user->set_Commodity_Index_Array(cur_cart_index,cur_cart_num);
			this->m_user->set_Commodity_Index_Num_Array(cur_cart_index_num,cur_cart_num);
			this->m_user->set_Balance(s_info->newbalance);
		}
		else
		{
			cout << "支付错误" << endl;
		}
		//后续更改本地操作
	}
	else
	{
		cout << "服务器繁忙，订单支付失败" << endl;
	}
	cin.get();
}

void Manager::show_Balance()
{
	cin.get();
	cout << "您的余额为:" << this->m_user->get_Balance() << "元" << endl;
}

void Manager::recharge_Amount(ClientSocket& c_socket)
{
	cout << "请输入你要充值的金额:" << endl;
	double charge_num = this->get_Mid_Num((double)MIN_CHARGE_NUM,(double)MAX_CHARGE_NUM);
	//构造通信子串
    info tinfo;
	//通信方及通信操作
	tinfo.com_src = CLINET_SEND;
	tinfo.opcode = _RECHARGE;
	//登录的账户名和密码
	strcpy(&tinfo.name[0],(char*)this->m_user->get_Name().data());
	strcpy(&tinfo.passwd[0],(char*)this->m_user->get_Passwd().data());
	//写入充值金额
	tinfo.newbalance = charge_num;
	//写入当前余额
	tinfo.balance = this->m_user->get_Balance();
	//获取长度
	c_socket.send_length = sizeof(tinfo);
	//清理缓存
	c_socket.clear_Cache();
	//将待发送信息写入缓存
	memcpy(c_socket.send_buf,&tinfo,c_socket.send_length);
	//发送并等待接收服务器的反馈
	bool getRecv = c_socket.send_To_Server();
	//收到反馈
	if(getRecv)
	{
		//判断type类型
		info* info_ptr = (info*)c_socket.recv_buf;
		switch (info_ptr->status_code)
		{
			//充值错误码
		case _RECHARGE_ERROR:
			cout << "充值失败" << endl;
			break;
			//充值成功
		case _RECHARGE_OK:
			cout << "充值成功" <<endl;
			this->m_user->set_Balance(info_ptr->balance);
			cout << "您现在余额为:" << this->m_user->get_Balance() << "元" << endl;
			break;
		default:
			cout << "未知错误" << endl;
			break;
		}
	}
	//服务器繁忙没有收到反馈
	else
	{
		cout << "服务器繁忙，充值失败" << endl;
	}
	cin.get();
}

bool Manager::change_Passwd(ClientSocket& c_socket)
{
	//吃掉回车
	cin.get();
	//输入旧密码
	cout << "请输入旧密码:" << endl;
	string old_passwd = this->get_Passwd();
	//密码验证成功
	if(old_passwd == this->m_user->get_Passwd())
	{
		while(true)
		{
			cout << "请输入新密码：" << endl;
			string new_passwd1 = this->get_Passwd();
			cout << "请再次输入密码进行确认：" << endl;
			string new_passwd2 = this->get_Passwd();
			//两次一致，同意修改密码
			if(new_passwd1 == new_passwd2)
			{
				//构造发送信息
				info s_info;
				s_info.opcode = _CHANGE_PASSWD;
				strcpy(&s_info.name[0],(char*)this->m_user->get_Name().data());
				strcpy(&s_info.passwd[0],(char*)old_passwd.data());
				strcpy(&s_info.newpasswd[0],(char*)new_passwd1.data());
				//获取长度
				c_socket.send_length = sizeof(s_info);
				//清理缓存
				c_socket.clear_Cache();
				//将待发送信息写入缓存
				memcpy(c_socket.send_buf,&s_info,c_socket.send_length);
				//发送并等待接收服务器的反馈
				bool getRecv = c_socket.send_To_Server();
				//收到反馈
				if(getRecv)
				{
					//判断type类型
					info* info_ptr = (info*)c_socket.recv_buf;
					switch (info_ptr->status_code)
					{
						//修改密码错误码
					case _CHANGE_PASSWD_ERROR:
						cout << "修改失败" << endl;
						break;
						//修改密码成功
					case _CHANGE_PASSWD_OK:
						cout << "修改成功" <<endl;
						this->m_user->set_Passwd(new_passwd1);
						cout << "请重新登录" << endl;
						//退出登录
						this->logout();
						//退出
						return true;
						break;
					default:
						cout << "未知错误" << endl;
						break;
					}
				}
				//服务器繁忙没有收到反馈
				else
				{
					cout << "服务器繁忙，充值失败" << endl;
				}
				return false;
			}
			else
			{
				cout << "两次密码不一致，请重新输入" << endl;
			}
		}
	}  
	//密码验证失败
	else
	{
		cout << "密码有误,修改密码失败" << endl;
	}
}

void Manager::logout()
{
	//将登录标志置零
	this->isLogin = false;
	//释放user
	if(this->m_user)
	{
		delete this->m_user;
		this->m_user = NULL;
	}
	cout << "您已经成功退出登录" << endl;
	cin.get();
}

void Manager::add_Commodity(ClientSocket& c_socket)
{
	commodity com;
	//输入id
	cout << "请输入商品identifier：" << endl;
	cin >> com.identifier;
	//验证id是否已经存在
	commodity com1;
	bool isExist = this->get_Com_By_Id(com.identifier,c_socket,com1);
	//该商品id已经存在，退出
	if(isExist)
	{
		cout << "该商品id已经存在，添加商品失败" << endl;
		cin.get();
		return;
	}
	//输入名字
	cout << "请输入商品名字：" << endl;
	string name;
	cin >> name;
	strcpy(com.cname,(char*)name.data());

	cout << "请输入品牌名称：" << endl; 
	string brand;
	cin >> brand;
	strcpy(com.brand,(char*)brand.data());

	cout << "请输入商品种类：" << endl;
	string classify;
	cin >> classify;
	strcpy(com.classify,(char*)classify.data());

	cout << "请输入价格：" << endl;
	com.price = this->get_Mid_Num(0.0,99999.0);

	cout << "请输入剩余数目：" << endl;
	cin >> com.remain; 
	com.frozen = 0; //创建商品时默认为0

	cout << "请输入其他额外信息：" << endl;
	string others;
	cin >> others;
	strcpy(com.others,(char*)others.data());

	//通信
	info* s_info = (info*)c_socket.send_buf;
	s_info->opcode = _ADD_COMMODITY;
	//将商品信息写入s_info
	s_info->com_array[0] = com;
	//发送
	bool getRecv = c_socket.send_To_Server();
	if(getRecv)
	{
		info* r_info = (info*)c_socket.recv_buf;
		if(r_info->status_code == _ADD_COMMODITY_OK)
		{
			cout << "添加成功" << endl;
		}
		else
		{
			cout << "未知错误，添加失败" << endl;
		}
	}
	else
	{
		cout << "服务器繁忙，添加失败" << endl;
	}
	cin.get();
}

void Manager::modify_Commodity(ClientSocket& c_socket)
{
	cout << "请输入你要修改的商品标识：" << endl;
	int iden;
	cin >> iden;
	//寻找该商品
	commodity com;
	bool isExist = this->get_Com_By_Id(iden,c_socket,com);
	if(isExist == false)
	{
		cout << "没有找到该商品，修改失败" << endl;
		cin.get();
		return;
	}
	//修改商品com
	Guide guide;
	//修改完成标志
	bool isFinish = false;
	while(!isFinish)
	{
		cout << "修改后商品信息如下" << endl;
		cout << "名称：" << com.cname << "\t"
		<< "品牌：" << com.brand << "\t"
		<< "分类：" << com.classify << "\t"
		<< "价格：" << com.price << "\t"
		<< "剩余数目：" << com.remain << "\t"
		<< "冻结数目：" << com.frozen << "\t"
		<< "其他信息：" << com.others << endl;
		cout << "-------------------------------------------------" << endl;
		guide.merchant_Modify_Com_Guide();
		cout << "请输入你的选择" << endl;
		int select = this->input_Choice();
		switch(select)
		{
			case MODIFY_COM_NAME:
			{
				cout << "请输入新的商品名称:" << endl;
				string name;
				cin >> name;
				strcpy(com.cname,(char*)name.data());
			}
				break;
			case MODIFY_COM_BRAND:
			{
				cout << "请输入新的商品品牌：" << endl;
				string brand;
				cin >> brand;
				strcpy(com.brand,(char*)brand.data());
			}
				break;
			case MODIFY_COM_CLASSIFY:
			{
				cout << "请输入新的商品种类：" << endl;
				string classify;
				cin >> classify;
				strcpy(com.classify,(char*)classify.data());
			}
				break;
			case MODIFY_COM_PRICE:
			{
				cout << "请输入新的价格：" << endl;
				double price;
				cin >> price;
				com.price = price;
			}
				break;
			case MODIFY_COM_REMAIN:
			{
				cout << "请输入新的剩余数目：" << endl;
				double remain;
				cin >> remain;
				com.remain = remain;
			}
				break;
			case MODIFY_COM_OTHERS:
			{
				cout << "请输入新的商品额外信息：" << endl;
				string others;
				cin >> others;
				strcpy(com.others,(char*)others.data());
			}
				break;
			case MODIFY_COM_FINISHED:
			{
				isFinish = true;
			}
				break;
			default:
			{
				cout << "非法输入" << endl;
			}
				break;
			
		}
		if(select != MODIFY_COM_FINISHED)
		{
			cin.get();
			system("clear");
		}
	}
	//通信
	info* s_info = (info*)c_socket.send_buf;
	//赋值
	s_info->opcode = _MODIFY_COMMODITY;
	s_info->com_array[0] = com;
	//发送
	bool getRecv = c_socket.send_To_Server();
	if(getRecv)
	{
		info* r_info = (info*)c_socket.recv_buf;
		if(r_info->status_code == _MODIFY_COMMODITY_OK)
		{
			cout << "修改成功" << endl;
		} 
		else
		{
			cout << "未知错误" << endl;
		}
	}
	else
	{
		cout << "服务器繁忙，修改商品失败" << endl;
	}
	cin.get();
}

string Manager::get_Name()
{
    string name;
    cin >> name;
    return name;
}

string Manager::get_Passwd()
{
    struct termios old,newp;
    tcgetattr(0,&old);
    newp = old;
    newp.c_lflag &= ~(ECHO|ICANON);

    while (true)
	{
		string passwd;
		char c;
		//输入密码
		while (true)
		{
			tcsetattr(0, TCSANOW, &newp);//进入循环将stdin设置为不回显状态
			scanf("%c",&c);//在不回显状态下输入密码
			tcsetattr(0, TCSANOW, &old);//每次输入一个密码的字符就恢复正常回显状态
            //输入换行结束
			if (c == '\n') break;
			passwd += c;
			cout << "*";
		}
        cout << endl;
		//验证密码强度
		if (this->judge_Passwd_Strength(passwd))
		{
			//return MD5().encrypt(passwd);
            return passwd;
		}
		else
		{
			cout << "您输入的密码强度不够,请重新输入" << endl;
		}
	}
	return string();
}

bool Manager::judge_Passwd_Strength(const string& passwd)
{
	//判断密码强度合格的三个标准
	bool num_Exist = false;
	bool alphabet_Exist = false;
	bool length_Is_Enough = false;
	unsigned int length = passwd.length();
	//cout << "开始判断强度,密码长度：" << length << endl;
	for (int i = 0; i < length; i++)
	{
		//cout << passwd.at(i) << endl;
		if (passwd.at(i) >= '0' && passwd.at(i) <= '9')
		{
			num_Exist = true;
			//cout << "有数字" << endl;
		}
		if (passwd.at(i) >= 'a' && passwd.at(i) <= 'z')
		{
			alphabet_Exist = true;
			//cout << "有字母" << endl;
		}
		if (passwd.at(i) >= 'A' && passwd.at(i) <= 'Z')
		{
			alphabet_Exist = true;
			//cout << "有字母" << endl;
		}
		if (num_Exist && alphabet_Exist)
		{
			break;
		}
	}
	if (length >= MIN_PASSWD_LENGTH)
	{
		length_Is_Enough = true;
		//cout << "长度合格" << endl;
	}
	return num_Exist & alphabet_Exist & length_Is_Enough;
}

bool Manager::get_Login_Statu()
{
	return this->isLogin;
}

USERTYPE Manager::get_User_Type()
{
	return this->m_user->get_Type();
}

int Manager::get_Mid_Num(int A,int B)
{
	int num;
	cout << "输入介于 " << A << " 和 " << B << " 之间" << endl;
	while(true)
	{
		cin >> num;
		if(num >= A && num <= B)
		{
			return num;
		}
		cout << "输入介于 " << A << " 和 " << B << " 之间" << endl;
		cout << "请重新输入:" << endl;
	}
}

double Manager::get_Mid_Num(double A,double B)
{
	double num;
	cout << "输入介于 " << A << " 和 " << B << " 之间" << endl;
	while(true)
	{
		cin >> num;
		if(num >= A && num <= B)
		{
			return num;
		}
		cout << "输入介于 " << A << " 和 " << B << " 之间" << endl;
		cout << "请重新输入:" << endl;
	}
}

bool Manager::get_Com_By_Id(int id,ClientSocket& c_socket,commodity& com)
{
	info s_info;
	s_info.opcode = _SEARCH_COM_BY_ID;
	s_info.identifier = id;
	//这里将用户名添加
	strcpy(&s_info.name[0],(char*)this->m_user->get_Name().data());
	//获取长度
	c_socket.send_length = sizeof(s_info);
	//清理缓存
	c_socket.clear_Cache();
	//将待发送信息写入缓存
	memcpy(c_socket.send_buf,&s_info,c_socket.send_length);
	//发送并等待接收服务器的反馈
	bool getRecv = c_socket.send_To_Server();
	//接收
	if(getRecv)
	{
		info* r_info = (info*)c_socket.recv_buf;
		int num = r_info->com_num;
		if(num == 0)
		{
			return false;
		}
		{
			// cout << "商品收到" << endl;
			// cout << "num:" << num << endl;
			// cout << "检索信息如下" << endl;
			// Guide().show_Com_Header();
			// 	cout <<r_info->com_array[0].identifier << "\t"
			// 		<< r_info->com_array[0].cname << "\t"
			// 		<< r_info->com_array[0].brand << "\t"
			// 		<< r_info->com_array[0].classify << "\t"
			// 		<< r_info->com_array[0].price << "\t"
			// 		<< r_info->com_array[0].remain << "\t"
			// 		<< r_info->com_array[0].frozen << "\t"
			// 		<< r_info->com_array[0].others << endl;
		}
		// 赋值
		com = r_info->com_array[0];
		return true;
	}
	else
	{
		cout << "服务器繁忙" << endl;
		return false;
	}

}

//------------功能函数区---------------
void Manager::show_struct_info(info tinfo)
{
	cout << "----------------------" << endl;
	cout << "com_src:" << tinfo.com_src << endl
		<< "opcode:" << tinfo.opcode << endl;
	printf("name:%s\n",tinfo.name);
	printf("passwd:%s\n",tinfo.passwd);
	cout << "----------------------" << endl;
}